package cn.wqk.rrtf.service;

import cn.wqk.rrtf.mapper.ArticleMapper;
import cn.wqk.rrtf.pojo.Article;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
@Service
@Transactional
public class ArticleServiceImpl implements ArticleService{
    @Autowired
    ArticleMapper articleMapper;
    @Override
    public List<Article> searchAllArticle() {

        return articleMapper.selectAllArticle();
    }

    @Override
    public List<Article> searchArticleByType(String articleType) {
        Article article=new Article();
        article.setType(articleType);
        return articleMapper.selectArticleByCondition(article);
    }

    @Override
    public Article searchArticleByAId(Integer articleId) {
        Article article=new Article();
        article.setArticleId(articleId);
        return articleMapper.selectArticleByCondition(article).get(0);
    }

    @Override
    public List<Article> searchMyPost(Integer userId) {
        return articleMapper.selectPublishArticleByUserId(userId);
    }

    @Override
    public List<Article> searchMyLatestPost(Integer userId) {
        List<Article> articleList = articleMapper.selectPublishArticleByUserId(userId);
        if(articleList.size()<=3){
            return articleList;
        }else{
            List<Article> articleList1=new ArrayList<Article>();
            for (int i=0;i<3;i++){
                articleList1.set(i,articleList.get(i));
            }
            return articleList1;
        }
    }

    @Override
    public List<Article> searchMyCare(Integer userId) {
        return articleMapper.selectCollectArticleByUserId(userId);
    }
}
